//
//  TabbarViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/8/23.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "TabbarViewController.h"

#import "MainViewController.h"
#import "BuyCarViewController.h"
#import "BuyHouseViewController.h"
#import "OrderViewController.h"
#import "MineViewController.h"

#import "NavViewController.h"
#import "LoginViewController.h"
@interface TabbarViewController ()

@end

@implementation TabbarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadUI];
    
}

- (void)loadUI{
    MainViewController *vc1 = [[MainViewController alloc] init];
    [self setupChildVc:vc1 title:@"首页" image:@"TIM图片20170810135854-1" selectedImage:@"TIM图片20170810135854"];
    
    BuyCarViewController *vc2 = [[BuyCarViewController alloc] init];
    [self setupChildVc:vc2 title:@"购车" image:@"车险1" selectedImage:@"车险"];
    
    BuyHouseViewController *vc3 = [[BuyHouseViewController alloc] init];
    [self setupChildVc:vc3 title:@"购房" image:@"房子0112" selectedImage:@"房子011"];
    
    OrderViewController *vc4 = [[OrderViewController alloc] init];
    [self setupChildVc:vc4 title:@"叫单" image:@"账单" selectedImage:@"账单2"];
    
    MineViewController *vc5 = [[MineViewController alloc] init];
    [self setupChildVc:vc5 title:@"我的" image:@"我的-(2)" selectedImage:@"我的2"];
}

//  初始化子控制器
- (void)setupChildVc:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage {
    vc.tabBarItem.title = title;
    vc.tabBarItem.image = [UIImage imageNamed:image];
    UIImage *imageHome = [UIImage imageNamed:selectedImage];
    imageHome = [imageHome imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    vc.tabBarItem.selectedImage = imageHome;
    
    // 设置 tabbarItem 选中状态下的文字颜色(不被系统默认渲染,显示文字自定义颜色)
    NSDictionary *dictHome = [NSDictionary dictionaryWithObject:[UIColor redColor] forKey:NSForegroundColorAttributeName];
    [vc.tabBarItem setTitleTextAttributes:dictHome forState:UIControlStateSelected];
    
    
    NavViewController *nav = [[NavViewController alloc] initWithRootViewController:vc];
    nav.navigationBarHidden = YES;
    
    [self addChildViewController:nav];
    
}

#pragma -mark UITabBarDelegate
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    if ([item.title isEqualToString:@"首页"]) {
        [user setObject:@"0" forKey:@"item"];
    }else if ([item.title isEqualToString:@"购车"]){
        [user setObject:@"1" forKey:@"item"];
    }else if ([item.title isEqualToString:@"购房"]){
        [user setObject:@"2" forKey:@"item"];
    }else if ([item.title isEqualToString:@"叫单"]){
        [user setObject:@"3" forKey:@"item"];
    }
    
    if ([item.title isEqualToString:@"我的"] && ![user objectForKey:@"userid"]) {
        LoginViewController *login = [[LoginViewController alloc] init];
        NavViewController *nav = [[NavViewController alloc] initWithRootViewController:login];
        nav.navigationBarHidden = YES;
        [self presentViewController:nav animated:YES completion:nil];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
